package com.google.android.gcm.demo.server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.xml.bind.DatatypeConverter;

public class recv {
	public static void main1() 
    {
            
            try
            {
                    System.out.println("create sock");
                    ServerSocket svsock = new ServerSocket(5000);
                    for (int i=0;;i++) {
                            System.out.println("accept");
                            Socket sock = svsock.accept();
                            System.out.println("buffer read");
                            int j = 0;
                            String four_bytes = "\\"+"x00"+"\\"+"x00"+"\\"+"x00"+"\\"+"x00";
                            String threegp_header = "\\"+"x00"+"\\"+"x00"+"\\"+"x00"+"\\"+"x18"+"\\"+"x66"+"\\"+"x74"+"\\"+"x79"+"\\"+"x70"+"\\"+"x33"+"\\"+"x67"+"\\"+"x70"+"\\"+"x34"+"\\"+"x00"+"\\"+"x00"+"\\"+"x03"+"\\"+"x00"+"\\"+"x33"+"\\"+"x67"+"\\"+"x70"+"\\"+"x34"+"\\"+"x33"+"\\"+"x67"+"\\"+"x70"+"\\"+"x36";

                            
                            FileOutputStream outFile = null;
                            try {
                                    //outFile = new FileOutputStream("test/xyz" + i + ".dat");
                                    //System.out.println("test/xyz" + i + ".dat");
                                    outFile = new FileOutputStream("C:/server/" + i + ".3gpp");
                                    System.out.println("test");
                                    System.out.println("C:/server/" + i + ".3gpp");
                                    j++;
                            } catch (IOException e1) {
                                    e1.printStackTrace();
                            }
                            InputStream is = sock.getInputStream();
                            byte[] byteBuffer = new byte[10*1024];
                            
                            outFile.write(threegp_header.getBytes());
                            outFile.write(four_bytes.getBytes());
                            //outFile.write(DatatypeConverter.parseHexBinary(threegp_header));
                            //outFile.write(DatatypeConverter.parseHexBinary(four_bytes));
                            
                            int allsize = 0;
                            while(sock.isConnected()) {
                                    int size = is.read(byteBuffer);
                                    if (size == -1){
                                            break;
                                    } else {
                                            outFile.write(byteBuffer, 0, size);
                                    }
                                    allsize += size;
                                    System.out.println("wd" + size);
                            }
                            System.out.println("close size=" + allsize);
                            outFile.close();
                            sock.close();
                    }
                    
            }
            catch(Exception e)
            {
                    e.printStackTrace();
            }       
            
            System.out.println("endmain");
    }
}
